package com.lanhe.admin.controller.banner;


import cn.hutool.core.util.ObjectUtil;
import com.lanhe.banner.domian.LhBanner;
import com.lanhe.banner.service.ILhBannerService;
import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * banner图管理(LhBanner)表控制层
 *
 * @author zhanggl
 * @since 2023-12-30 13:58:55
 */
@RestController
@RequestMapping("lhBanner")
public class LhBannerController extends BaseController {
    
    /**
     * 服务对象
     */
    @Resource
    private ILhBannerService lhBannerService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhBanner banner) {
        startPage();
        List<LhBanner> list = lhBannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    /**
     * 新增
     *
     * @param banner 实体
     * @author zhanggl
     * @date 2023/12/4
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:add')")
    @Log(title = "Banner管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhBanner banner) {
        banner.setCreateBy(getUsername());
        banner.setCreateTime(new Date());
        return toAjax(lhBannerService.insertBanner(banner));
    }

    /**
     * 查询单个
     *
     * @param bannerId 包ID
     * @author zhanggl
     * @date 2023/12/4
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:query')")
    @GetMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable Long bannerId)
    {
        if (null == bannerId){
            return error("ID不能为空");
        }
        return success(lhBannerService.selectBannerById(bannerId));
    }

    /**
     * 修改
     *
     * @param banner 实体
     * @author zhanggl
     * @date 2023/12/4
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:edit')")
    @Log(title = "Banner管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LhBanner banner) {
        if (null == banner.getBannerId()){
            return error("ID不能为空");
        }
        banner.setUpdateBy(getUsername());
        banner.setUpdateTime(new Date());
        return toAjax(lhBannerService.updateBanner(banner));
    }

    /**
     * 删除
     *
     * @param bannerId 包ID
     * @author zhanggl
     * @date 2023/12/4
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:remove')")
    @Log(title = "Banner管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerId}")
    public AjaxResult remove(@PathVariable Long bannerId)
    {
        if (ObjectUtil.isEmpty(bannerId)){
            return error("ID不能为空");
        }
        return toAjax(lhBannerService.deleteBannerById(bannerId));
    }
}

